package com.itheima.consumer.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Map;

@Component
@Slf4j
public class SpringRabbitListener {

    @RabbitListener(queues = "simple.queue")
    public void onMessage(Message message) {
        log.info("监听到simple.queue的消息id:【{}】 ", message.getMessageProperties().getMessageId());
        log.info("监听到simple.queue的消息【{}】 ", new String(message.getBody()));
        throw new RuntimeException("故意抛出异常");
    }

    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(20);
    }

    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2........接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String message) {
        System.out.println("消费者1接收到消息: " + message);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String message) {
        System.out.println("消费者2接收到消息: " + message);
    }

    //    @RabbitListener(queues = "direct.queue1")
//    public void listenDirectQueue1(String message) {
//        System.out.println("消费者1接收到消息: " + message);
//    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1", durable = "true"),
            exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT, durable = "true"),
            key = {"red", "blue"}
    ))
    public void listenDirectQueue1(String message) {
        System.out.println("消费者1接收到消息: " + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2", durable = "true"),
            exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT, durable = "true"),
            key = {"red", "yellow"}
    ))
    public void listenDirectQueue2(String message) {
        System.out.println("消费者2接收到消息: " + message);
    }

    @RabbitListener(queues = "topic.queue1")
    public void listentopicQueue1(String message) {
        System.out.println("topic.queue[china.#]接收到消息: " + message);
    }

    @RabbitListener(queues = "topic.queue2")
    public void listentopicQueue2(String message) {
        System.out.println("topic.queue[#.news]接收到消息: " + message);
    }

    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(Map<String,Object> message) {
        System.out.println("接收到object.queue的消息: " + message);
    }

    // 死信队列

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "dead.queue", durable = "true"),
            exchange = @Exchange(name = "dlx.exchange", type = ExchangeTypes.DIRECT, durable = "true"),
            key = {"dlx.routingkey"}
    ))
    public void listenDeadLetterQueue(String message) {
        System.out.println("接收到死信队列【dead.queue】的消息: " + message);
    }

    // 声明一个延迟消息的队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue", durable = "true"),
            exchange = @Exchange(name = "delay.exchange",delayed = "true"),
            key = {"delay.routingkey"}
    ))
    public void listenDelayQueue(String message) {
        System.out.println("接收到延迟队列【delay.queue】的消息: " + message);
    }
}
